package com.yingside.jdbc.crm;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoPassWordTxt;
import com.lovo.netCRM.component.LovoTxt;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private LovoTxt txtName;
    private LovoPassWordTxt txtPassword;
    private LovoButton btnLogin,btnCancel;
    private UserDao userDao = new UserDao();
    public LoginFrame(){
        this.setTitle("登录");
        this.setBounds(200,200,300,200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        initComponent();
    }

    private void initComponent() {
        txtName = new LovoTxt("用户名:", 50,50,this);
        txtPassword = new LovoPassWordTxt("密  码:", 50,80,this);
        btnLogin = new LovoButton("登录",50,130,this);
        btnCancel = new LovoButton("取消",150,130,this);

        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = txtName.getText();
                String password = new String(txtPassword.getPassword());

                int login = userDao.login(username, password);
                if(login > 0){
                    JOptionPane.showMessageDialog(null,"登录成功!");
                    EmployeeFrame employeeFrame = new EmployeeFrame();
                    employeeFrame.setVisible(true);
                    LoginFrame.this.dispose();
                }
                else{
                    JOptionPane.showMessageDialog(null,"登录失败!");
                }

            }
        });

    }

    public static void main(String[] args) {
        LoginFrame loginFrame = new LoginFrame();
        loginFrame.setVisible(true);
    }
}
